import { readFileSync } from 'node:fs'
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript';
import postcss from 'rollup-plugin-postcss';
import dts from 'rollup-plugin-dts';

const pkg = JSON.parse(readFileSync('./package.json'));

export default [
  {
    input: './src/index.ts',
    output: [
      {
        file: pkg.main,
        format: 'cjs',
        sourcemap: true
      },
      {
        file: pkg.module,
        format: 'esm',
        sourcemap: true
      }
    ],
    plugins: [
      resolve(),
      commonjs(),
      typescript({
        tsconfig: './tsconfig.app.json',
        /** 导出声明文件 */
        declaration: true,
        /** 导出声明文件目录 */
        declarationDir: 'types',
        /** 输出目录 */
        outDir: 'dist',
        compilerOptions: {
          lib: ['es5', 'es6', 'dom'],
          target: 'es5',
          rootDir: 'src'
        }
      }),
      postcss(),
    ]
  },
  {
    input: './dist/esm/types/index.d.ts',
    output: [{file: './dist/index.d.ts', format: 'esm'}],
    plugins: [dts()],
    external: [/\.(css|less|scss)$/],
  },
]